﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Web.Security;
using BitPortal.Business;
using BitPortal.Models;
using BitPortal.Utils;
namespace BitPortal.Admin.Page.Module
{
    public partial class PageAddEdit : Controls.BaseAdminControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!Roles.IsUserInRole("RootAdmin"))
            {
                Panel1.Hidden = false;
                Panel1.Dispose();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnInsert_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("PageInsert");
                Models.PortalPage g = new Models.PortalPage()
                {
                    Name = txtName.Text,
                    SortOrder = txtSortOrder.Text.ToInt32TryParse(),
                    IsEnabled = ckIsEnabled.Checked,
                    Layout = cbLayout.SelectedItem.Value,
                    Meta = new Meta() { MetaTitle = txtMetaTitle.Text, MetaDescription = txtMetaDescription.Text, MetaKeyword = txtMetaKeyword.Text }
                };
                //Logs
                g.Logs.DatetimeCreated = g.Logs.DatetimeEdited = DateTime.Now;
                g.Logs.CreatingUser = g.Logs.EditingUser = (Membership.GetUser() == null) ? null : (Guid?)new Guid(Membership.GetUser().ProviderUserKey.ToString());
                //Only Allow RootAdmin
                if (Roles.IsUserInRole("RootAdmin"))
                {
                    g.IsBasePage = ckIsBasePage.Checked;
                    if (ckIsBasePage.Checked)
                    {
                        g.ClassName = txtClassCode.Text;
                    }
                }
                if (BitPortal.Business.PageBLL.Insert(g) > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Thêm trang thành công!");
                    Window1.Hide();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("PageInsert");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                int PortalPageID = txtID.Text.ToInt32TryParse();
                BitPortal.Models.PortalPage g = bitEnt.PortalPages.SingleOrDefault(ent => ent.PageID == PortalPageID);
                if (g != null)
                {
                    g.Name = txtName.Text;
                    g.SortOrder = Convert.ToInt32(txtSortOrder.Text);
                    g.IsEnabled = ckIsEnabled.Checked;
                    g.Layout = cbLayout.SelectedItem.Value;
                    //
                    g.Logs.DatetimeEdited = DateTime.Now;
                    g.Logs.EditingUser = (Membership.GetUser() == null) ? null : (Guid?)new Guid(Membership.GetUser().ProviderUserKey.ToString());
                    //
                    g.Meta = new Meta() { MetaTitle = txtMetaTitle.Text, MetaDescription = txtMetaDescription.Text, MetaKeyword = txtMetaKeyword.Text };
                    //Only Allow RootAdmin
                    if (Roles.IsUserInRole("RootAdmin"))
                    {
                        g.IsBasePage = ckIsBasePage.Checked;
                        if (ckIsBasePage.Checked)
                        {
                            g.ClassName = txtClassCode.Text;
                        }
                    }
                }
                if (bitEnt.SaveChanges() > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Sửa trang thành công!");
                    Window1.Hide();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        public void BindData(int PortalPageID = 0)
        {
            if (PortalPageID == 0)
            {
                txtName.Text = txtMetaDescription.Text = txtMetaKeyword.Text = txtMetaTitle.Text = "";
                txtID.Text = txtSortOrder.Text = "0";

                btnUpdate.Hidden = true;
                btnInsert.Hidden = false;
                Window1.Show();
                return;
            }
            Models.PortalPage g = Business.PageBLL.GetByID(PortalPageID);
            if (g != null)
            {
                txtID.Text = g.PageID.ToString();
                txtName.Text = g.Name;
                cbLayout.SelectedItem.Value = g.Layout;
                txtSortOrder.Text = g.SortOrder.ToString();
                ckIsEnabled.Checked = g.IsEnabled;

                txtMetaTitle.Text = g.Meta.MetaTitle;
                txtMetaDescription.Text = g.Meta.MetaDescription;
                txtMetaKeyword.Text = g.Meta.MetaKeyword;
                //Only Allow RootAdmin
                if (Roles.IsUserInRole("RootAdmin"))
                {
                    ckIsBasePage.Checked = g.IsBasePage;
                    txtClassCode.Text = g.ClassName;
                    Panel1.Hidden = false;
                }
                else
                {
                    Panel1.Hidden = true;
                }
                btnUpdate.Hidden = false;
                btnInsert.Hidden = true;
                Window1.Show();
            }
        }
    }
}